package com.smzd.ccms.complets;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.smzd.ccms.business.ChannelService;
import com.smzd.ccms.core.BaseCompletPlugin;
import com.smzd.ccms.core.ParamsWrapper;
import com.smzd.ccms.domain.Channel;

public class BreadcrumbComplet extends BaseCompletPlugin {
	ChannelService channelService;

	public BreadcrumbComplet(int id) {
		super(id);
		channelService = ChannelService.getInstance();
	}

	@Override
	public String execute(ParamsWrapper params) throws Exception {
		if (channelService == null) {
			synchronized (this) {
				if (channelService == null) {
					channelService = ChannelService.getInstance();
				}
			}
		}
		Channel channel;
		// 获取配置的父级栏目ID，可能不存在，这时要自己获取去
		Integer chId = params.getParamValue("chId", 0);
		if (chId != null && chId > 0) {// 存在，太好了
			channel = channelService.getBreadcrumbChannels(chId);
		} else {
			String cPath = params.getModelParamValue("_channel_");
			String sPath = params.getModelParamValue("_site_");
			channel = channelService.getBreadcrumbChannelsByPath(sPath, cPath);
		}
		List<Channel> channels = new ArrayList<Channel>();
		if(channel!=null){
			channels.add(channel);
			while(channel.getParent()!=null){
				channel = channel.getParent();
				channels.add(channel);
			}
			if(channels.size()>1){
				Collections.reverse(channels);
			}
		}
		params.put("_channels", channels);
		return params.getParamValue("view", "breadcrumb.ftl");
	}
}